﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO.Ports;
using System.Threading;
using WriteCode.IngenuBorder;
using System.Net.Sockets;

namespace WriteCode
{
    public partial class MainWindow : Window
    {
        /// <summary>
        /// 盈利工装板编址协议
        /// </summary>
        internal enum AddressCmd_Enum : byte
        {
            /// <summary>
            /// 通讯的帧头
            /// </summary>
            Header = 0x68,
            /// <summary>
            /// 编址的命令
            /// </summary>
            WriteCommand = 0x22,
            /// <summary>
            /// 读取地址的命令
            /// </summary>
            ReadCommand = 0x11,
            /// <summary>
            /// 读取ADC的命令
            /// </summary>
            ADCCommand = 0x10,
            /// <summary>
            /// 通讯的帧尾
            /// </summary>
            Last = 0x16,
            /// <summary>
            /// 默认地址
            /// </summary>
            Address = 0x00,
            /// <summary>
            /// 编址命令反码
            /// </summary>
            WriteFcommand = 0xdd,
            /// <summary>
            /// 读取地址命令的反码
            /// </summary>
            ReadFcommand = 0xee,


        }
        private IngenuBorder.Ingenu?ingenu;
        public MainWindow()
        {
            InitializeComponent();
        }
        
        /// <summary>
        /// 确认串口并打开
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Measure_Click(object sender, RoutedEventArgs e)
        {
            string result = string.Empty;
            string portname = SerailPort.Text;
            int baudRate = 9600; // 提供 baudRate 参数
            Parity parity = Parity.None; // 提供 parity 参数
            ingenu = new Ingenu(ref result, portname, baudRate, parity);
        }
        /// <summary>
        /// 双击下拉框更新串口列表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ComboBox_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            var portlist = SerialPort.GetPortNames(); //把列表绑定到串口下拉列表框中
            SerailPort.ItemsSource = portlist;
            SerailPort.SelectedIndex = 0;
        }
        /// <summary>
        /// 探测器工装板编址
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Wtite_Click(object sender, RoutedEventArgs e)
        {
            string err_info = string.Empty;
            byte[] receive = new byte[8];
            if (ingenu is null) { MessageBox.Show("请配置串口信息！"); return; }
            if(AddressCode.Text == string.Empty) { MessageBox.Show("请输入工装地址！"); return; }
            ingenu.SendData(ingenu.Display_vWriteAddressInfo(Convert.ToByte(AddressCode.Text)), ref err_info);
            receive = ingenu.Receive_Wait(50, 8,out err_info);
            if (!string.IsNullOrEmpty(err_info))
            {
                MessageBox.Show(err_info);
            }
            else
            {
                if (receive[2] == (byte)AddressCmd_Enum.WriteCommand)
                {
                    IngenuAddressResult.Text = AddressCode.Text + ":号地址工装板编写成功";
                }
                else
                {
                    IngenuAddressResult.Text = "编写失败，请检查工装连接！" + err_info;
                }
            }
        }
        /// <summary>
        /// 读取工装板地址
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Read_Click(object sender, RoutedEventArgs e)
        {
            IngenuAddressResult.Clear();
            int address = 0;
            if (ingenu is null) { MessageBox.Show("请配置串口信息！"); return; }
            byte[] receive = new byte[8];
            string err_info = string.Empty;
            ingenu.SendData(ingenu.Display_vQureyAddressInfo(),ref err_info);
            receive = ingenu.Receive_Wait(50, 8,out err_info);
            if (!string.IsNullOrEmpty(err_info))
            {
                MessageBox.Show(err_info);
            }
            else
            {
                if (receive[2] == (byte)AddressCmd_Enum.ReadCommand)
                {
                    address = Convert.ToInt16(receive[5]);
                    IngenuAddressResult.Text = "读取成功，当前工装板地址为：" + address.ToString();
                }
                else
                {
                    address = 0;
                    IngenuAddressResult.Text = "读取失败，请检查工装连接！" + err_info;
                }
            }
            
        }
        /// <summary>
        /// 限制地址文本框数字范围
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (!int.TryParse(AddressCode.Text, out int number))
            {
                AddressCode.Visibility = Visibility.Visible;
            }
            else
            {
                number = Math.Min(number, 128);
                AddressCode.Text = number.ToString();
            }
        }
        /// <summary>
        /// 读取工装板电压
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Btn_ReadClick(object sender, RoutedEventArgs e)
        {
            IngenuAddressResult.Clear();
            int adc_value = 0;
            byte[] Get_ADCBytes = new byte[2];
            if (ingenu is null) { MessageBox.Show("请配置串口信息！"); return; }
            byte[] receive = new byte[17];
            string err_info = string.Empty;
            ingenu.SendData(ingenu.Display_vReadADC(1), ref err_info);//1号工装板读取ADC
            receive = ingenu.Receive_Wait(50, receive.Length, out err_info);
            if (!string.IsNullOrEmpty(err_info))
            {
                MessageBox.Show(err_info);
            }
            else
            {
                if (receive[2] == (byte)AddressCmd_Enum.ADCCommand)
                {
                    Get_ADCBytes[1] = receive[13];
                    Get_ADCBytes[0] = receive[14];
                    adc_value = BitConverter.ToInt16(Get_ADCBytes,0);
                    IngenuAddressResult.Text = "读取成功，当前ADC电压为：" + adc_value.ToString() + "mV";
                }
                else
                {
                    IngenuAddressResult.Text = "读取失败，请检查工装连接！" + err_info;
                }
            }
        }
    }
}
